package com;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yuhang
 * @desc
 * @since 2022/10/31
 */
public class Test10 {

    public static void main(String args[]) {

        Test10 java8Tester = new Test10();
//        Integer value1 = null;
//        Integer value2 = new Integer(10);
//
//        // Optional.ofNullable - 允许传递为 null 参数
//        Optional<Integer> a = Optional.ofNullable(value1);
//
//        // Optional.of - 如果传递的参数是 null，抛出异常 NullPointerException
//        Optional<Integer> b = Optional.of(value2);
//        System.out.println(java8Tester.sum(a, b));
//        A ax = null;// new Test10().new A("a", "b", "c");
//        Optional<A> op = Optional.ofNullable(ax);
//
//
//        op.ifPresent(o -> o.setA("123"));
//
//
//        System.out.println(op.orElse(new Test10().new A("22","22","222")));


//        java8Tester.y();

//        Optional<Object> supplierId = Optional.of(Optional.ofNullable(1L).orElse(0L));
//
//        System.out.println(supplierId.get());


        for (int i = 0; i < 100; i++) {
            if (i > 10) {

                if (i == 15) {
                    break;
                }

                System.out.println(i);
            }
        }
    }

    public void y() {
        A x = new A("1", "2", "3");

        A g = Objects.requireNonNull(x, "xxxx");

    }

    public void x() {
        List<A> deptList = new ArrayList<>();
        deptList.add(new A("1", "2", "3"));
        deptList.add(new A("1", "21", "3"));
        deptList.add(new A("1", "22", "32"));
        Optional<A> deptOptional = deptList.stream()
                .filter(e -> e.getA().equals("1")).findFirst();
        A x = Objects.requireNonNull(new A("1", "2", "3"));
//        System.out.println(x);
//
//        System.out.println(deptOptional.isPresent());


        Double a = new Double("256");
        Double b = 256D;

        boolean g = Objects.equals(a, b);
        System.out.println("xxx:" + g);

        List<String> as = deptList.stream().map(A::getB).collect(Collectors.toList());


        System.out.println(as);
    }

    private class A {
        private String a;
        private String b;
        private String c;

        public A(String a, String b, String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }

        public String getC() {
            return c;
        }

        public void setC(String c) {
            this.c = c;
        }

        @Override
        public String toString() {
            return "A{" +
                    "a='" + a + '\'' +
                    ", b='" + b + '\'' +
                    ", c='" + c + '\'' +
                    '}';
        }
    }

    public Integer sum(Optional<Integer> a, Optional<Integer> b) {

        // Optional.isPresent - 判断值是否存在

        System.out.println("第一个参数值存在: " + a.isPresent());
        System.out.println("第二个参数值存在: " + b.isPresent());

        // Optional.orElse - 如果值存在，返回它，否则返回默认值
        Integer value1 = a.orElse(new Integer(0));

        //Optional.get - 获取值，值需要存在
        Integer value2 = b.get();
        return value1 + value2;
    }
}
